package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/18 16:02
 */
@SuppressWarnings("all")
@Service
public class DtsCommentServiceImpl implements DtsCommentService {
    @Autowired
    DtsCommentMapper dtsCommentMapper;


    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId, List<Integer> brandIds) {

        IPage<DtsComment> iPage = new Page<>(page,limit);

        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();
        //判断排序规则
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc("c."+sort);
        }else{
            wrapper.orderByAsc("c."+sort);
        }
        //判断用户ID和商品ID是否为空
        if(userId!=null){
            wrapper.eq("c.user_id",userId);
        }
        //判断商品ID是否为空
        if(valueId!=null){
            wrapper.eq("c.value_id",valueId);
        }
        //品牌id条件
        if(brandIds != null && brandIds.size() > 0) {
            wrapper.in( "g.brand_id", brandIds);
        }

        dtsCommentMapper.selectCommentAndBrandPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items", iPage.getRecords());
        return map;
    }

    @Override
    public void deleteById(DtsComment dtsComment) {
        //删除对应的评论
        dtsCommentMapper.deleteById(dtsComment.getId());
    }
}
